#include "record.h"
#include "ui_record.h"

#include "lib/sql.h"

#include <qmessagebox.h>

Record::Record(QWidget *parent,int f) :
    QWidget(parent),
    ui(new Ui::Record)
{
    fla = f;
    ui->setupUi(this);
    ui->tableView->setModel(&m_mode);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

Record::~Record()
{
    delete ui;
}

void Record::initPage(QString strCondition)
{
    if(fla == 1){
        ui->btn_delrecord->hide();
    }
    auto l = Sql::getinstance()->getrecord(strCondition);
    m_mode.clear();
    m_mode.setHorizontalHeaderLabels(QStringList{"图书编号","用户名","书名","借阅时间","还书时间"});

    for(int i=0;i<l.size();i++){
        QList<QStandardItem*> item;
        for(int j=0;j<l[i].size();j++){
            item.append(new QStandardItem(l[i][j]));
        }
        m_mode.appendRow(item);
    }
}


void Record::on_btn_return_clicked()
{
    int r = ui->tableView->currentIndex().row();
    if(r < 0){
        return;
    }
    else{
        auto id = m_mode.item(r,0)->text();
        Sql::getinstance()->rebook(id);
        initPage();

    }
}


void Record::on_btn_delrecord_clicked()
{
    int r = ui->tableView->currentIndex().row();
    if(r < 0){
        QMessageBox::information(nullptr,"信息","无选择图书");
    }
    else{
        auto id = m_mode.item(r,0)->text();
        Sql::getinstance()->clrearecord(id);
        initPage();
    }
}


void Record::on_lineEditrecord_textChanged(const QString &arg1)
{
    QString strSql = QString("where book_name like '%%1%' or name like '%%1%'").arg(arg1);
    initPage(strSql);
}

